﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace IPConfig
{
    public class XMLHelper
    {
        private XmlDocument ipConfigXML;
        /// <summary>
        /// 加载配置文件
        /// </summary>
        public XMLHelper()
        {
            if (!File.Exists("IPConfig.xml"))
            {
                CeateConfig();
            }
            XmlDocument _ipConfigXML = new XmlDocument();
            _ipConfigXML.Load("./IPConfig.xml");
            ipConfigXML = _ipConfigXML;
        }
        /// <summary>
        /// 创建配置文件
        /// </summary>
        public void CeateConfig()
        {
            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(dec);
            XmlElement root = xml.CreateElement("root");
            xml.AppendChild(root);

            for (int i = 1; i <= 3; i++)
            {
                XmlElement IPConfig = xml.CreateElement("IPConfig" + i);
                root.AppendChild(IPConfig);
                XmlElement IP = xml.CreateElement("IP" + i);
                XmlElement ZW = xml.CreateElement("ZW" + i);
                XmlElement WG = xml.CreateElement("WG" + i);
                XmlElement DNSF = xml.CreateElement("DNSF" + i);
                XmlElement DNSS = xml.CreateElement("DNSS" + i);

                IPConfig.AppendChild(IP);
                IPConfig.AppendChild(ZW);
                IPConfig.AppendChild(WG);
                IPConfig.AppendChild(DNSF);
                IPConfig.AppendChild(DNSS);
            }
            xml.Save("./IPConfig.xml");

        }
        /// <summary>
        /// 获取单个配置具体项信息
        /// </summary>
        /// <param name="configName">配置名</param>
        /// <returns></returns>
        public string GetIPConfigStr(string configName)
        {
            return ipConfigXML[configName].InnerText;
        }
        /// <summary>
        /// 获取某个配置信息
        /// </summary>
        /// <param name="XH">配置序号</param>
        /// <returns></returns>
        public IPConfigModel GetIPConfigModel(int XH)
        {
            XmlElement xmlElement = ipConfigXML["root"]["IPConfig" + XH];
            IPConfigModel model = new IPConfigModel();
            model.IP = xmlElement["IP" + XH].InnerText;
            model.ZW = xmlElement["ZW" + XH].InnerText;
            model.WG = xmlElement["WG" + XH].InnerText;
            model.DNSF = xmlElement["DNSF" + XH].InnerText;
            model.DNSS = xmlElement["DNSS" + XH].InnerText;
            return model;
        }
        /// <summary>
        /// 设置配置信息
        /// </summary>
        /// <param name="XH">配置名</param>
        /// <param name="model">值</param>
        /// <returns></returns>
        public bool SetIPConfigModel(int XH, IPConfigModel model)
        {
            try
            {
                XmlElement xmlElement = ipConfigXML["root"]["IPConfig" + XH];
                xmlElement["IP" + XH].InnerText = model.IP;
                xmlElement["ZW" + XH].InnerText = model.ZW;
                xmlElement["WG" + XH].InnerText = model.WG;
                xmlElement["DNSF" + XH].InnerText = model.DNSF;
                xmlElement["DNSS" + XH].InnerText = model.DNSS;
                ipConfigXML.Save("./IPConfig.xml");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
